/*
 * @Date: 2024-11-27 16:03:54
 * @LastEditors: lyx
 * @LastEditTime: 2024-12-24 15:04:11
 * @FilePath: \sup-tpt-shell-engineering\src\app.tsx
 * @Description:
 */
import React, { Component, PropsWithChildren } from "react";
import { Provider } from "react-redux";
import { store } from "./store";
import "./app.less";
import Taro, { getCurrentInstance } from "@tarojs/taro";

class App extends Component<PropsWithChildren> {
  onLaunch() {
    // Taro.hideTabBar();
    const { router } = getCurrentInstance();
    const { params } = router || {};
    console.log('onLaunch params', params);
    const { inviteCode = "", openId = "" } = params || {};
    if (inviteCode) {
      Taro.setStorage({
        key: "inviteCode",
        data: inviteCode,
      });
    }
    if (openId) {
      Taro.setStorage({
        key: "openId",
        data: openId,
      });
    }
  }

  componentDidShow() {
    // 在这里我们可以判断小程序是否从后台重新打开
    const { router = {} } = getCurrentInstance();
    const { path = "" } = (router || {}) as any;
    console.log("小程序 componentDidShow path", path);

    if (path !== "pages/index/index") {
      const currentTime = Date.now();
      const lastHiddenTime = Taro.getStorageSync('lastHiddenTime');
      if (!lastHiddenTime || currentTime - lastHiddenTime > 30 * 60 * 1000) {
        console.log("需要重定向到 首页");
        // 替换 'pages/index/index' 为你的首页路径
        Taro.reLaunch({
          url: "/pages/index/index", // 到首页
        });
      }
    }
  }

  componentDidHide() {
    // 隐藏的时候 存入当前的 时间
    const currentTime = Date.now();
    Taro.setStorageSync('lastHiddenTime', currentTime);
  }

  // 在 App 类中的 render() 函数没有实际作用
  // 请勿修改此函数
  render() {
    return <Provider store={store}>{this.props.children}</Provider>;
  }
}

export default App;
